package com.jsu.action;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.jsu.domain.Product;
import com.jsu.service.IProductService;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage("struts-default")
@Namespace("/")
public class ProductAction extends ActionSupport {
	private static final long serialVersionUID = -4507950040864751830L;
	private static final Logger LOG = LogManager.getLogger(ProductAction.class);
	
	@Autowired
	private IProductService service;
	
	public ProductAction() {
		super();
		
		LOG.debug("in ProductAction.<init>: Printing stack trace for: " + this);
//		StackTraceElement[] elements = Thread.currentThread().getStackTrace();
//		for (int i = 1; i < elements.length; i++) {
//		     StackTraceElement s = elements[i];
//		     LOG.debug("\tat " + s.getClassName() + "." + s.getMethodName() + "(" + s.getFileName() + ":" + s.getLineNumber() + ")");
//		}
	}

	private List<Product> products;

	@Action(value="list_products", results={@Result(name="success",location="/list_products.jsp"),
			@Result(name="error",location="/error.jsp")})
	public String execute() {
		LOG.debug("in ProductAction.execute(), service: " + service.getClass());
		products = service.findAll();
		return SUCCESS;
	}

	public List<Product> getProducts() {
		return products;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}

	public IProductService getService() {
		return service;
	}

	public void setService(IProductService service) {
		LOG.debug("in ProductAction.setService: Printing stack trace for: " + this);
//		StackTraceElement[] elements = Thread.currentThread().getStackTrace();
//		for (int i = 1; i < elements.length; i++) {
//		     StackTraceElement s = elements[i];
//		     LOG.debug("\tat " + s.getClassName() + "." + s.getMethodName() + "(" + s.getFileName() + ":" + s.getLineNumber() + ")");
//		}

		this.service = service;
	}
}